/*
                 \\|//
                 (o o)
--------------ooO-(_)-Ooo-------------------------------------------------------
 
   (C)  Copyright 2004 By Gregory A. Prentice - All Rights Reserved.
   http://www.cafechess.org
  
   Redistribution and use in source and binary forms, with or without 
   modification, are permitted provided that the following conditions are met:
   
    - Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
   
    - Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
   
    - If you decide to use the code in a publicly released product then please
      give the author credit for his work.
  
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA,  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY,WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   
                      Ooo.
--------------.ooO----(  )------------------------------------------------------
              (  )    (_/
               \_)
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

using Cafechess.Imaging;
using Cafechess.Chess.General;

namespace Cafechess.Chess.Controls
{
  /// <summary>
  /// Summary description for BoardSquare.
  /// </summary>
  public class BoardSquare : Layer
  { 
    #region Fields
    private Pieces coPiece;
    private bool coColor;
    private static readonly int BACKGROUND = 0;
    private static readonly int HIGHLIGHT = 1;
    private static readonly int PIECE = 2;
#if POCKET_PC
    ImageAttributes coAttributes = new ImageAttributes();
#endif

    #endregion Fields

    #region Properties

    public Bitmap Highlight
    {
      get
      { 
        DrawableBitmap bm = Get(HIGHLIGHT) as DrawableBitmap;
        return bm.Drawing;
      }
      set
      {
        DrawableBitmap bm = Get(HIGHLIGHT) as DrawableBitmap;
        bm.Drawing = value;
      }
    }
    public Bitmap Background
    {
      get
      { 
        DrawableBitmap bm = Get(BACKGROUND) as DrawableBitmap;
        return bm.Drawing;
      }
      set
      {
        DrawableBitmap bm = Get(BACKGROUND) as DrawableBitmap;
        bm.Drawing = value;
      }
    }
    
    public Bitmap PieceImage
    {
      get
      { 
        DrawableBitmap bm = Get(PIECE) as DrawableBitmap;
        return bm.Drawing;
      }
      set
      {
        DrawableBitmap bm = Get(PIECE) as DrawableBitmap;
        bm.Drawing = value;
      }
    }

    override public Rectangle Square
    {
      get{ return base.Square;}
      set
      { 
        base.Square = value;
        foreach(IDrawable idraw in this )
        {
          DrawableBitmap bm = idraw as DrawableBitmap;
          if(bm != null)
            bm.Square = Square;
        }
      }
    }

    public Pieces  Piece
    {
      get{ return coPiece;}
      set{ coPiece=value;}
    }

    public bool Color
    {
      set{coColor = value;}
      get{return coColor;}
    }
    #endregion Properties;

    #region Constructors

    public BoardSquare()
    {      
      Add(new DrawableBitmap(BACKGROUND.ToString()));
      Add(new DrawableBitmap(HIGHLIGHT.ToString()));
      Add(new DrawableBitmap(PIECE.ToString()));
    }
    
    public BoardSquare(Bitmap oBackground, Rectangle oRectangle)
    {
      Background = oBackground;
      Square = oRectangle;
    }

    #endregion Constructors
    public void drawPiece(System.Drawing.Graphics graphics, Rectangle aSquare)
    {
      IDrawable idraw = Get(PIECE);
      idraw.Square = aSquare;
      idraw.Render(graphics);
      idraw.Square = Square;
      idraw = Get(HIGHLIGHT);
      idraw.Render(graphics);
      //graphics.DrawImage(Highlight,Square);
    }
    public void drawPiece(System.Drawing.Graphics graphics)
    {
      if (Piece != Pieces.None)
      {
        IDrawable idraw = Get(PIECE);
        idraw.Render(graphics);
      }

    }
    public void drawBackground(System.Drawing.Graphics graphics)
    {
      IDrawable idraw = Get(BACKGROUND);
      idraw.Render(graphics);
      //graphics.DrawImage(Highlight,Square);
    }
    public void drawHighlight(System.Drawing.Graphics graphics)
    {
      IDrawable idraw = Get(HIGHLIGHT);
      idraw.Render(graphics);
        //graphics.DrawImage(Highlight,Square);
    }
  }
}